package com.woniuxy.kaer.service.consumer;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.commons.util.Data;
import com.woniuxy.kaer.commons.util.Result;
import com.woniuxy.kaer.redis.util.RedisKey;
import com.woniuxy.kaer.service.entity.ServiceEvaluate;
import com.woniuxy.kaer.service.entity.ServiceOrder;
import com.woniuxy.kaer.service.mapper.ServiceEvaluateMapper;
import com.woniuxy.kaer.service.mapper.ServiceOrderMapper;
import com.woniuxy.kaer.tenants.dto.ShopInfoDto;
import com.woniuxy.kaer.tenants.feign.TenantsFeignClient;
import com.woniuxy.kaer.tenants.from.UpdateShopScoreFrom;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class ShopPointsCalculatedConsumer {
    @Resource
    private ServiceOrderMapper serviceOrderMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private TenantsFeignClient tenantsFeignClient;
    @Resource
    private ServiceEvaluateMapper serviceEvaluateMapper;
    @RabbitListener(queues = "shop_points_calculated_queue")
    @RabbitHandler
    public void judgeOrderExpired(String serviceOrderId)throws Exception{
        System.out.println("商铺评分，服务次数做更新");
        ServiceOrder serviceOrder = serviceOrderMapper.selectById(serviceOrderId);
        Result<ShopInfoDto> result = tenantsFeignClient.getShopInfoByShopId(serviceOrder.getShopId());
        ShopInfoDto shopInfoDto = result.getData();
        if(shopInfoDto==null){
            throw new Exception("店铺不存在");
        }
        QueryWrapper<ServiceEvaluate> serviceEvaluateQueryWrapper = new QueryWrapper<>();
        //serviceEvaluateQueryWrapper.eq("shop_id",serviceOrder.getShopId());
        serviceEvaluateQueryWrapper.eq("service_order_id",serviceOrderId);
        //这条订单
        ServiceEvaluate serviceEvaluate = serviceEvaluateMapper.selectOne(serviceEvaluateQueryWrapper);
        //查询总条数
        QueryWrapper<ServiceEvaluate> serviceEvaluateQueryWrapper1 = new QueryWrapper<>();
        serviceEvaluateQueryWrapper1.eq("shop_id",serviceOrder.getShopId());
        Long aLong = serviceEvaluateMapper.selectCount(serviceEvaluateQueryWrapper1);
        //计算新的店铺评分
        Double newShopScore = (shopInfoDto.getShopScore()+Double.valueOf(serviceOrder.getEvaluateScore().doubleValue())) /Double.valueOf(aLong);
        UpdateShopScoreFrom updateShopScoreFrom = new UpdateShopScoreFrom();
        updateShopScoreFrom.setShopId(serviceOrder.getShopId());
        updateShopScoreFrom.setShopScore(newShopScore);
        tenantsFeignClient.updateShopScore(updateShopScoreFrom);
    }
}
